package Thread;
import java.util.Scanner;
/**notifyAll 唤醒所有线程*/
public class demo25 {
    public static void main(String[] args) {

    Object locker = new Object();
    Thread t1 = new Thread(() -> {
        try {
            System.out.println("t1 wait 之前");
            synchronized (locker) {
                locker.wait();
            }
            System.out.println("t1 wait 之后");
        } catch (InterruptedException e) {
            throw new RuntimeException();
        }
    });
    Thread t2 = new Thread(() -> {
        try {
            System.out.println("t2 wait 之前");
            synchronized (locker) {
                locker.wait(10000);
            }
            System.out.println("t2 wait 之后");
        } catch (InterruptedException e) {
            throw new RuntimeException();
        }
    });

    Thread t3 = new Thread(() -> {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入任意内容, 唤醒所有线程");
        scanner.next();
        synchronized (locker) {
            locker.notifyAll();
        }
    });

    t1.start();
    t2.start();
    t3.start();
    }
}
/*wait和sleep的“联系”：
* wait和sleep都有等待时间这个选项
* 都可以被提前唤醒*/


/*wait和sleep的区别：
* wait必须要搭配锁，但是sleep不需要
* 如果在synchronized内部使用，wait释放锁，但是sleep不会释放锁*/